package com.example.demo.dao;

/**
 * 数据库管理员信息操作接口实现类
 */

import com.example.demo.bean.Admin;
import com.example.demo.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;

import java.sql.SQLException;

public class AdminDaoImpl implements AdminDao {

    @Override
    public Admin adminLogin(Admin admin) {
        //创建QueryRunner
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        //准备SQL语句
        String sql = "select * from manager where username = ? and password = ?";
        //设置占位符参数
        Object[] param = {admin.getUsername(),admin.getPassword()};
        //
        try {
            Object[] query = queryRunner.query(sql, new ArrayHandler(), param);
            if(query.length > 2) {
                return new Admin((String) query[1], (String) query[2]);
            }
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        return null;
    }
}
